﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanderlaarsBonnen;

namespace HanderlaarsBonnen.Controllers
{ 
    public class WonController : Controller
    {
        private VoucherDbEntities1 db = new VoucherDbEntities1();

        //
        // GET: /Won/

        public ViewResult Index()
        {
            var gewonnenvoucher = db.Gewonnenvoucher.Include("Consumer").Include("Voucher");
            return View(gewonnenvoucher.ToList());
        }

        //
        // GET: /Won/Details/5

        public ViewResult Details(int id)
        {
            Gewonnenvoucher gewonnenvoucher = db.Gewonnenvoucher.Single(g => g.WonvoucherID == id);
            return View(gewonnenvoucher);
        }

        //
        // GET: /Won/Create

        public ActionResult Create()
        {
            ViewBag.ConsumerId = new SelectList(db.Consumer, "ConsumerId", "Email");
            ViewBag.VoucherId = new SelectList(db.Voucher, "VoucherId", "Field");
            return View();
        } 

        //
        // POST: /Won/Create

        [HttpPost]
        public ActionResult Create(Gewonnenvoucher gewonnenvoucher)
        {
            if (ModelState.IsValid)
            {
                db.Gewonnenvoucher.AddObject(gewonnenvoucher);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ConsumerId = new SelectList(db.Consumer, "ConsumerId", "Email", gewonnenvoucher.ConsumerId);
            ViewBag.VoucherId = new SelectList(db.Voucher, "VoucherId", "Field", gewonnenvoucher.VoucherId);
            return View(gewonnenvoucher);
        }
        
        //
        // GET: /Won/Edit/5
 
        public ActionResult Edit(int id)
        {
            Gewonnenvoucher gewonnenvoucher = db.Gewonnenvoucher.Single(g => g.WonvoucherID == id);
            ViewBag.ConsumerId = new SelectList(db.Consumer, "ConsumerId", "Email", gewonnenvoucher.ConsumerId);
            ViewBag.VoucherId = new SelectList(db.Voucher, "VoucherId", "Field", gewonnenvoucher.VoucherId);
            return View(gewonnenvoucher);
        }

        //
        // POST: /Won/Edit/5

        [HttpPost]
        public ActionResult Edit(Gewonnenvoucher gewonnenvoucher)
        {
            if (ModelState.IsValid)
            {
                db.Gewonnenvoucher.Attach(gewonnenvoucher);
                db.ObjectStateManager.ChangeObjectState(gewonnenvoucher, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ConsumerId = new SelectList(db.Consumer, "ConsumerId", "Email", gewonnenvoucher.ConsumerId);
            ViewBag.VoucherId = new SelectList(db.Voucher, "VoucherId", "Field", gewonnenvoucher.VoucherId);
            return View(gewonnenvoucher);
        }

        //
        // GET: /Won/Delete/5
 
        public ActionResult Delete(int id)
        {
            Gewonnenvoucher gewonnenvoucher = db.Gewonnenvoucher.Single(g => g.WonvoucherID == id);
            return View(gewonnenvoucher);
        }

        //
        // POST: /Won/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Gewonnenvoucher gewonnenvoucher = db.Gewonnenvoucher.Single(g => g.WonvoucherID == id);
            db.Gewonnenvoucher.DeleteObject(gewonnenvoucher);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}